%include "inc/support.inc"
%include "inc/ports.inc"
%include "basic.inc"
	
	bits 16
	
	org BOOT_SEG

start:
	cli
	;;  enable a20 line
	FAST_A20_ENABLE

	sti

	;; set BOOT_SEG environment

	mov ax, cx
	mov ds, ax
	mov ss, ax
	mov es, ax
	mov sp, BOOT_SEG 	; set bottom of stack

	call clear_screen
	mov si, hello
	call print_message

	call check_int13h_extension
	test ax, 0
	jnz next
	mov si, support_msg
	call print_message

next:	jmp $

;;;==================================
;;;	  clean screen
clear_screen:
	pusha
	mov ax, 0x0600
	xor cx, cx
	xor bh, 0x0f
	mov dh, 24
	mov dl, 79
	int 0x10

set_cursor_position:
	mov ah, 02
	mov bh, 0
	mov dx, 0
	int 0x10
	popa
	ret

;;; ==================================
;;; print message
print_message:
	pusha
	mov ah, 0x0e
	xor bh, bh

do_print_message_loop:
	lodsb
	test al, al
	jz do_print_message_done
	int 0x10
	jmp do_print_message_loop

do_print_message_done:
	popa
	ret

;;; =================================
;;; check_int13_extension()
;;;
check_int13h_extension:
	push bx
	mov bx, 0x55aa
	mov ah, 0x41

	int 0x13
	setc al
	jc do_check_int13h_extension_done
	cmp bx, 0xaa55
	setnz al
	jnz do_check_int13h_extension_done
	test cx, 1
	setz al
do_check_int13h_extension_done:
	pop bx
	movzx ax, al
	ret

hello db 'hello, world', 13, 10, 0
support_msg db 'support int13 extension', 13, 10, 0
	times	510-($-$$) db 0	;
      dw 0xaa55
	